﻿using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Servsers
{
    public class AddressServers
    {
        private readonly IRepository<address> addressRepository;

        public AddressServers()
        {
            addressRepository = new DapperRepository<address>();

        }

        internal List<SelectListItem> GROUP_IDData()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = addressRepository.SqlWithParamsDynamic("select a.GROUP_ID Value,a.GROUP_NAME Text from address_group  a  ");
            tables.Clear();
            //tables.Add(new SelectListItem { Value = "0", Text = "代码主分类" });
            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }

            return tables;
        }

        internal void Add(address model)
        {
            addressRepository.Insert(model);
        }

        internal void Update(address model)
        {
            addressRepository.Update(model);
        }

        internal address GetModelById(string id, string groupId, string keyWord)
        {
            address address = new address();
            string sqlWhere = "";
            if (string.IsNullOrEmpty(id))
            {
                if(string.IsNullOrEmpty(groupId) || groupId == "default")
                {
                    sqlWhere += " where GROUP_ID is null ";
                    if (!string.IsNullOrEmpty(keyWord))
                    {
                        sqlWhere += " and PSN_NAME like '%" + keyWord.Trim() + "%' ";
                    }
                }
                else if (groupId == "sg")
                {
                    if (!string.IsNullOrEmpty(keyWord))
                    {
                        sqlWhere += " where PSN_NAME like '%" + keyWord.Trim() + "%' ";
                    }
                }
                else
                {
                    sqlWhere += " where GROUP_ID = " + groupId;
                    if (!string.IsNullOrEmpty(keyWord))
                    {
                        sqlWhere += " and PSN_NAME like '%" + keyWord.Trim() + "%' ";
                    }
                }
                address = addressRepository.SqlWithParams("select * from address " + sqlWhere).FirstOrDefault();
            }
            else
            {
                address = addressRepository.GetById(id);
            }
            return address;
        }

        internal IList<address> GetModelByGroupId(string groupId, string keyWord)
        {
            IList<address> addressList = new List<address>();
            string sqlWhere = "";
            if(string.IsNullOrEmpty(groupId) || groupId == "default")
            {
                sqlWhere += " where GROUP_ID is null ";
                if (!string.IsNullOrEmpty(keyWord))
                {
                    sqlWhere += " and PSN_NAME like '%" + keyWord.Trim() + "%' ";
                }
            }
            else if(groupId == "sg")
            {
                if (!string.IsNullOrEmpty(keyWord))
                {
                    sqlWhere += " where PSN_NAME like '%" + keyWord.Trim() + "%' ";
                }
            }
            else
            {
                sqlWhere += " where GROUP_ID = " + groupId;
                if (!string.IsNullOrEmpty(keyWord))
                {
                    sqlWhere += " and PSN_NAME like '%" + keyWord.Trim() + "%' ";
                }
            }

            addressList = addressRepository.SqlWithParams("select * from address " + sqlWhere).ToList(); 
            return addressList;
        }

        internal bool UpdateGroupById(int addId,int? groupId)
        {
            bool flag = false;
            if(groupId == null)
            {
                flag = addressRepository.InsertUpdateOrDeleteSqlBool("update address set GROUP_ID = null where ADD_ID = " + addId);
            }
            else
            {
                flag = addressRepository.InsertUpdateOrDeleteSqlBool("update address set GROUP_ID = " + groupId + " where ADD_ID = " + addId);
            }
            return flag;
        }
    }
}